//
//  ShoppingDetailViewController.m
//  JewelryStore
//
//  Created by apple on 2016/11/7.
//  Copyright © 2016年 com. All rights reserved.
//

#import "ShoppingDetailViewController.h"
#import "TitleImageButton.h"
#import "HomeScrollView.h"
#import "BaseLabel.h"
#import "ShopHaopinView.h"
#import "ShopDetailCell.h"
#import <UIView+Toast.h>
#import "ShopAddressView.h"

#define SHOPDETAILCELL @"shopDetailCell"

@interface ShoppingDetailViewController ()<UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate>
@property (nonatomic, strong) UITableView *shoppingDetailTableView;


@property (nonatomic, copy) NSArray *imageURL; //假数据
@property (nonatomic, copy) NSArray *dataSource;
@end

@implementation ShoppingDetailViewController

#pragma mark - ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupNavigationBar];
    [self setupBottomBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.shoppingDetailTableView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.shoppingDetailTableView.tableHeaderView = [self setupSectionUIWithReusableView];
}


#pragma mark - 懒加载

- (UITableView *)shoppingDetailTableView
{
    if (!_shoppingDetailTableView) {
        
        _shoppingDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-49-64)];
        
        _shoppingDetailTableView.delegate = self;
        
        _shoppingDetailTableView.dataSource = self;
        
        _shoppingDetailTableView.backgroundColor = kBackgroundGray;
        
        _shoppingDetailTableView.showsVerticalScrollIndicator = NO;
        
        _shoppingDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_shoppingDetailTableView registerNib:[UINib nibWithNibName:@"ShopDetailCell" bundle:nil] forCellReuseIdentifier:SHOPDETAILCELL];
        
    }
    return _shoppingDetailTableView;
}
- (NSArray *)imageURL
{
    if (!_imageURL) {
        _imageURL = @[@"http://pic.4j4j.cn/upload/pic/20130815/31e652fe2d.jpg", @"http://pic.4j4j.cn/upload/pic/20130909/681ebf9d64.jpg", @"http://img3.imgtn.bdimg.com/it/u=581864004,1701839937&fm=21&gp=0.jpg"];
    }
    return _imageURL;
}

- (NSArray *)dataSource
{
    if (!_dataSource) {
        
        _dataSource = @[@"", @"", @"", @"", @"", @""];
    }
    return _dataSource;
}

#pragma mark - table dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:SHOPDETAILCELL];
    
    cell.model = @[];
    
    cell.fd_enforceFrameLayout = NO;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:SHOPDETAILCELL cacheByIndexPath:indexPath configuration:^(id cell) {
        
    }];
}
#pragma mark - private

- (void)setupNavigationBar
{
    self.title = @"卡罗蒂戒指";
    self.view.backgroundColor = kWhite;
    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithImage:KImageNamed(@"share") style:UIBarButtonItemStyleDone target:self action:@selector(shareClick)];
    self.navigationItem.rightBarButtonItem = shareItem;
}

- (void)setupBottomBar
{
    //背景
    UIView *backView = [[UIView alloc] init];
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.and.bottom.equalTo(@0);
        make.height.equalTo(@49);
    }];
    
    //客服
    TitleImageButton *serviceBtn = [[TitleImageButton alloc] initWithTitle:@"客服" image:KImageNamed(@"service_btn") systemFont:Font(12)];
    [serviceBtn addTarget:self action:@selector(serviceClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:serviceBtn];
    [serviceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.bottom.equalTo(@0);
        make.width.equalTo(@(SCREEN_WIDTH/6.5));
    }];
    
    //收藏
    TitleImageButton *favoriteBtn = [[TitleImageButton alloc] initWithTitle:@"收藏" image:KImageNamed(@"favorite_btn") systemFont:Font(12)];
    [favoriteBtn addTarget:self action:@selector(favoriteClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:favoriteBtn];
    [favoriteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(serviceBtn.mas_right);
        make.top.and.bottom.equalTo(@0);
        make.width.equalTo(serviceBtn.mas_width);
    }];
    
    //购物车
    TitleImageButton *carBtn = [[TitleImageButton alloc] initWithTitle:@"购物车" image:KImageNamed(@"car_btn") systemFont:Font(12)];
    [carBtn addTarget:self action:@selector(carBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:carBtn];
    [carBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(favoriteBtn.mas_right);
        make.top.and.bottom.equalTo(@0);
        make.width.equalTo(favoriteBtn.mas_width);
    }];
    
    //加入购物车
    TitleImageButton *joinCar = [[TitleImageButton alloc] initWithTitle:@"加入购物车" backgroundColor:[UIColor orangeColor] systemFont:Font(17)];
    [joinCar addTarget:self action:@selector(joinCarClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:joinCar];
    [joinCar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(carBtn.mas_right);
        make.top.and.bottom.equalTo(@0);
        make.width.equalTo(@(SCREEN_WIDTH*3.5/13));
    }];
    
    //立即购买
    TitleImageButton *buyNow = [[TitleImageButton alloc] initWithTitle:@"立即购买" backgroundColor:[UIColor redColor] systemFont:Font(17)];
    [buyNow addTarget:self action:@selector(buyNowClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:buyNow];
    [buyNow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(joinCar.mas_right);
        make.top.and.bottom.equalTo(@0);
        make.width.equalTo(joinCar.mas_width);
    }];
}

- (UIView *)setupSectionUIWithReusableView
{
    CGFloat headHeight = SCREEN_HEIGHT/15 +10 + SCREEN_HEIGHT/6 + SCREEN_HEIGHT/3.5 + SCREEN_HEIGHT/8.8;
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, headHeight)];

    //轮播图
    CGRect scFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/3.5);
    HomeScrollView *shoppingSC = [[HomeScrollView alloc] initWithFrame:scFrame imageURLStringsGroup:self.imageURL delegate:self isAutoScroll:NO];
    [headView addSubview:shoppingSC];
    
    //大背景
    UIView *backgroudView = [[UIView alloc] init];
    backgroudView.backgroundColor = RGB(242, 243, 247);
    [headView addSubview:backgroudView];
    [backgroudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(SCREEN_HEIGHT/3.5));
        make.left.and.right.and.bottom.equalTo(@0);
    
    }];
    
    //标题背景
    UIView *backTitleView = [[UIView alloc] init];
    backTitleView.backgroundColor = kWhite;
    [backgroudView addSubview:backTitleView];
    [backTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.right.equalTo(@0);
        make.height.equalTo(@(SCREEN_HEIGHT/6));
    }];
    
    //标题
    BaseLabel *titleLab = [[BaseLabel alloc] initWithTitle:@"卡罗蒂情侣戒指女925银活口对戒大家撒打发" color:[UIColor blackColor] font:Font(17)];
    [backTitleView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.and.top.equalTo(@10);
    }];
    
    //价格
    BaseLabel *priceLab = [[BaseLabel alloc] initWithTitle:@"￥189888" color:[UIColor redColor] font:Font(17)];
    [backTitleView addSubview:priceLab];
    [priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLab.mas_bottom).offset(10);
        make.left.equalTo(titleLab.mas_left);
    }];
    
    //市场价格
    BaseLabel *priceOldLab = [[BaseLabel alloc] initWithTitle:@"市场价:￥20999" color:[UIColor grayColor] font:Font(13)];
    priceOldLab.attributedText = [priceOldLab setupCenterLineWithOldStr:priceOldLab.text];
    [backTitleView addSubview:priceOldLab];
    [priceOldLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(priceLab.mas_centerY);
        make.left.equalTo(priceLab.mas_right).offset(10);
    }];
    
    //款号
    BaseLabel *styleLab = [[BaseLabel alloc] initWithTitle:@"款号:NE3003" color:[UIColor blackColor] font:Font(14)];
    [backTitleView addSubview:styleLab];
    [styleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(priceOldLab.mas_bottom).offset(10);
        make.left.equalTo(priceLab.mas_left);
    }];
    
    //快递
    BaseLabel *sendPriceLab = [[BaseLabel alloc] initWithTitle:@"快递:00" color:[UIColor lightGrayColor] font:Font(14)];
    [backTitleView addSubview:sendPriceLab];
    [sendPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(styleLab.mas_left);
        make.top.equalTo(styleLab.mas_bottom).offset(10);
        make.width.equalTo(@((SCREEN_WIDTH-20)/3));
        make.bottom.equalTo(@-5);
    }];
    
    //销量
    BaseLabel *numLab = [[BaseLabel alloc] initWithTitle:@"月销56" color:[UIColor lightGrayColor] font:Font(14)];
    numLab.textAlignment = UIBaselineAdjustmentAlignCenters;
    [backTitleView addSubview:numLab];
    [numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sendPriceLab.mas_right);
        make.top.equalTo(sendPriceLab.mas_top);
        make.width.equalTo(sendPriceLab.mas_width);
    }];
    
    //所在地
    BaseLabel *addressLab = [[BaseLabel alloc] initWithTitle:@"深圳" color:[UIColor lightGrayColor] font:Font(14)];
    addressLab.textAlignment = UIBaselineAdjustmentNone;
    [backTitleView addSubview:addressLab];
    [addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(numLab.mas_right);
        make.top.equalTo(numLab.mas_top);
        make.width.equalTo(numLab.mas_width);
    }];
    
    //地址背景
    UIView *backAddressView = [[UIView alloc] init];
    [backgroudView addSubview:backAddressView];
    backAddressView.backgroundColor = kWhite;
    [backAddressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backTitleView.mas_bottom).offset(5);
        make.left.and.right.equalTo(@0);
        make.height.equalTo(@(SCREEN_HEIGHT/8.8));
    }];
    
    //送至
    BaseLabel *sendToLab = [[BaseLabel alloc] initWithTitle:@"送至" color:[UIColor lightGrayColor] font:Font(14)];
    [backAddressView addSubview:sendToLab];
    [sendToLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(styleLab.mas_left);
        make.top.equalTo(@20);
    }];
    
    //送至按钮
    TitleImageButton *sendToBtn = [[TitleImageButton alloc] initWithDefaultTitle:@" 广东省深圳市罗湖区田贝四路翠竹酒店5楼" image:KImageNamed(@"location-pin") systemFont:Font(14)];
    sendToBtn.enabled = YES;
    [sendToBtn addTarget:self action:@selector(sendToClick) forControlEvents:UIControlEventTouchUpInside];
    [backAddressView addSubview:sendToBtn];
    [sendToBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sendToLab.mas_right).offset(10);
        make.centerY.equalTo(sendToLab.mas_centerY);
    }];
    
    //售后
    TitleImageButton *afterBtn = [[TitleImageButton alloc] initWithDefaultTitle:@"店铺发货&售后" image:KImageNamed(@"tishi") systemFont:Font(12)];
    afterBtn.isSelected = YES;
    [backAddressView addSubview:afterBtn];
    [afterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sendToLab.mas_left);
        make.top.equalTo(sendToLab.mas_bottom).offset(10);
        make.bottom.equalTo(@-5);
    }];
    
    //货到
    TitleImageButton *huodaoBtn = [[TitleImageButton alloc] initWithDefaultTitle:@"货到付款" image:KImageNamed(@"tishi") systemFont:Font(12)];
    [backAddressView addSubview:huodaoBtn];
    [huodaoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backAddressView.mas_centerX);
        make.bottom.equalTo(@-5);
        make.centerY.equalTo(afterBtn.mas_centerY);
    }];
    
    //不可七天退换
    TitleImageButton *tuihuanBen = [[TitleImageButton alloc] initWithDefaultTitle:@"不可七天退换" image:KImageNamed(@"tishi") systemFont:Font(12)];
    [backAddressView addSubview:tuihuanBen];
    [tuihuanBen mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(addressLab.mas_right);
        make.bottom.equalTo(@-5);
        make.centerY.equalTo(afterBtn.mas_centerY);
    }];
    
    //评价背景
    UIView *backCommentView = [[UIView alloc] init];
    backCommentView.backgroundColor = kWhite;
    [backgroudView addSubview:backCommentView];
    [backCommentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backAddressView.mas_bottom).offset(5);
        make.left.and.right.equalTo(@0);
        make.height.equalTo(@(SCREEN_HEIGHT/15));
    }];
    
    //客户评价
    NSString *count = @"333";
    NSString *text = [NSString stringWithFormat:@"客户评价(%@)", count];
    BaseLabel *kehulab = [[BaseLabel alloc] initWithTitle:text color:[UIColor lightGrayColor] font:Font(14)];
    [backCommentView addSubview:kehulab];
    [kehulab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.centerY.equalTo(backCommentView.mas_centerY);
    }];
    
    //好评度
    ShopHaopinView *haopin = [[NSBundle mainBundle] loadNibNamed:@"ShopHaopinView" owner:nil options:nil].firstObject;
    haopin.font = Font(14);
    haopin.haoPinValue.text = @"38%";
    [backCommentView addSubview:haopin];
    [haopin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.and.top.and.bottom.equalTo(@0);
        make.width.equalTo(@(SCREEN_WIDTH/3.7));
    }];
    
    if (self.dataSource.count == 0) {
        [haopin showEmptyStatus];
    }
    
    haopin.tapBlock = ^(){
        TT_Log(@"goToHaoPin");
    };
    
    return headView;
}

- (void)shareClick
{
    TT_Log(@"shareClick");
}

- (void)serviceClick
{
    TT_Log(@"serviceClick");
}

- (void)favoriteClick
{
    TT_Log(@"favoriteClick");
}

- (void)carBtnClick
{
    TT_Log(@"carBtnClick");
}

- (void)joinCarClick
{
    TT_Log(@"joinCarClick");
    CSToastStyle *toastStyle = [[CSToastStyle alloc] initWithDefaultStyle];
    
    toastStyle.messageColor = [UIColor redColor];
    
    toastStyle.cornerRadius = 5;
    
    [self.view makeToast:@"成功加入购物车" duration:2 position:CSToastPositionCenter style:toastStyle];
    
}

- (void)buyNowClick
{
    TT_Log(@"buyNowClick");
}

- (void)sendToClick
{
    TT_Log(@"sendToClick");
    ShopAddressView *address = [[ShopAddressView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        
    address.model = @[@""];
    
    address.update = ^(NSString *text)
    {
        TT_Log(@"%@", text);
    };
    
    [address showAddressView];
        
}
@end
